﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.IO;
using System.Drawing;
/// <summary>
/// Summary description for WebService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
// [System.Web.Script.Services.ScriptService]
public class WebService : System.Web.Services.WebService
{

    public WebService()
    {

        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }

    /// <summary>
    /// 保存图片
    /// </summary>
    /// <param name="buff"></param>
    /// <param name="filename"></param>
    /// <param name="albumID"></param>
    /// <param name="msg"></param>
    /// <returns></returns>
    [WebMethod]
    public  bool SaveImg(byte[] buff,string filename,int albumID,ref string msg)
    {
        try
        {
            var stream=new MemoryStream(buff);
            
            var img = System.Drawing.Image.FromStream(stream);
            string path = Server.MapPath("~/album/a" + albumID.ToString());
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            img.Save(path +"/"+ filename);
            stream.Close();
            stream = null;
            return true;
        }
        catch (Exception ex) {
            msg = ex.Message;
            return false;
        }
    }

    /// <summary>
    /// 删除图片
    /// </summary>
    /// <param name="filename"></param>
    /// <param name="albumID"></param>
    /// <param name="msg"></param>
    /// <returns></returns>
    [WebMethod]
    public bool DelImg(string filename,int albumID,ref string msg)
    {
        try
        {
            string path = Server.MapPath("~/album/a" + albumID.ToString())+ "/" + filename;

            if (File.Exists(path)) {
                File.Delete(path);
            }
            return true;
        }
        catch (Exception ex)
        {
            msg = ex.Message;
            return false;
        }
    }



}

